<?php
class OrdersController extends AppController {

	var $name = 'Orders';

	function admin_index() {
	    //debug($this->paginate());
		$this->Order->recursive = 3;
		$this->set('orders', $this->paginate());
	}
    function beforeFilter() {		
		parent::beforeFilter();
		$this->layout="admin";		
		$this->Auth->authorize = 'controller';		
	}

    function isAuthorized() {
		$MY_RULE_ID = 7;
        if ($this->action == 'login' || $this->action == 'logout') {            
            return true;
        }else{
			$allow = $this->requestAction('ruleStaffTypes/allow/'.$MY_RULE_ID);
			if ($allow){
				return true;
			}else{
				$this->Session->setFlash(__('Permission denied!',true));
				$this->redirect(array("controller"=>"staffs","action"=>"login","admin"=>false));
				return false;
			}
        }
        return false;
    }
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid order', true));
			$this->redirect(array('action' => 'index'));
		}
        $this->Order->recursive = 4;
		$this->set('order', $this->Order->read(null, $id));
            //debug($this->Order->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Order->create();
			if ($this->Order->save($this->data)) {
				$this->Session->setFlash(__('The order has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The order could not be saved. Please, try again.', true));
			}
		}
		$agents = $this->Order->Agent->find('list');
		$this->set(compact('agents'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid order', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Order->save($this->data)) {
				$this->Session->setFlash(__('The order has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The order could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Order->read(null, $id);
		}
		$agents = $this->Order->Agent->find('list');
		$this->set(compact('agents'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for order', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Order->delete($id)) {
			$this->Session->setFlash(__('Order deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Order was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>